package com.bos.controller;

import com.bos.data.model.BosUserModel;
import com.bos.data.model.UserTicket;
import com.bos.service.AccessTokenService;
import com.bos.service.Contacts_UserService;
import com.bos.service.MTAuthorizationService;
import com.bos.util.QiWeiXinParamesUtil;
import com.bos.util.WeiXinUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/*
 * 这是一个进行扫码登陆的类
 *
 * */
@Controller
public class AccessTokenController {
    @Autowired
    @Qualifier("accessTokenServiceImpl")
    AccessTokenService accService;

    // 进行扫码登陆的操作
    @RequestMapping(value = "/getCondition")
    public String getWerixin(HttpServletRequest request,
                             HttpSession session,
                             @RequestParam("code") String code,
                             @RequestParam("state") String state, ModelAndView mv, Model model) {
        try {
            System.out.println(code);
            System.out.println(state);
            // 得到扫码登陆传过来的code 和state
            // 根据code获取用户信息
            // 1.查询出AccessToken中的值，看是否有应用 AccessToken,无，则调用请求的方法，有则来判断有效时间有没有过期
            String accessToken = WeiXinUtil.getAccessToken(accService,
                    "app", QiWeiXinParamesUtil.corpId,
                    QiWeiXinParamesUtil.agentSecret);
            Contacts_UserService cus = new Contacts_UserService();
            cus.getUser2(code, accessToken);
            System.out.println("----222---" + accessToken);
            String name = Contacts_UserService.USERNAME;
            System.out.println("用户名为——----" + name);
            String avatar=Contacts_UserService.AVATARS;
            System.out.println("头像为——----" + avatar);
            BosUserModel user = accService.findUserByName(name);
            session.setAttribute("user", user);
            //根据名字查询到当前这条数据
            return "index1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "a";
    }

    //2.企业微信通讯录同步
   /* @RequestMapping(value = "getUser")
    public String getUser(Model model) {
        //声明一个存放多个user对象的集合
        List<HashMap<String, Object>> userList = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> user = null;
        //1查询出access_Token的值
        //List<AccessToken> acce = accService.findAccessToken();
        //for(AccessToken a:acce){
        String access_token = WeiXinUtil.getAccessToken(accService, "contacts", QiWeiXinParamesUtil.corpId, QiWeiXinParamesUtil.contactsSecret);
        //获取部门列表信息(不填则是查询出所有的部门列表)
        Contacts_DepartmentService departService = new Contacts_DepartmentService();
        List<String> depts = departService.getDepartmentList(access_token, "");
        //根据部门信息去获取成员的详细信息(查询到的数据)
        Contacts_UserService userService = new Contacts_UserService();
        List<BosUserModel> users = userService.getDepartmentUserDetails(depts, access_token, "1");
        for (BosUserModel bosUserModel : users) {
            bosUserModel.setPassword("123456");
        }
        //将其保存到数据库中
        accService.insertUsers(users);
        //重定向到首页查询出所有数据
        //重定向到首页查询出所有数据
        return "redirect:showAll";
    }
*/
    //3.手机端免授权登陆
    @RequestMapping(value = "/MTAuthorization")
    public String mTAuthorization(HttpServletRequest request,HttpSession session) {
        String code= request.getParameter("code");
        String state=request.getParameter("state");

        MTAuthorizationService mts=new MTAuthorizationService();
        String accessToken=WeiXinUtil.getAccessToken(QiWeiXinParamesUtil.corpId, QiWeiXinParamesUtil.agentSecret,"app").getToken();
        //获取成员信息
        JSONObject userInfo=mts.getUserInfo(accessToken, code);

        //获取成员详情
        UserTicket userTicket=new UserTicket();
        userTicket.setUser_ticket(userInfo.getString("user_ticket"));

        JSONObject userDetail=mts.getUserDetail(accessToken, userTicket);
        System.out.println("PageController-------"+userDetail);
        //根据userid来获取user的信息
        String name=userDetail.get("name").toString();
        System.out.println("name-------"+name);
        BosUserModel user = accService.findUserByName(name);
        session.setAttribute("user", user);
        return "redirect:pcmt";
    }

}
